/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.operationrecord;

import mbean.admin.problem.*;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Inject;
import mbean.admin.solution.SolutionViewMB;
import model.entity.Attendance;
import model.entity.Customerdemand;
import model.entity.Operationrecord;
import model.entity.Problem;
import model.entity.Solution;
import model.entity.Task;
import model.entity.TaskCustomerdemand;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.primefaces.model.LazyDataModel;
import service.ICustomerdemandManager;
import service.InterfaceServiceLocal;
import utils.PageDateModel;
import utils.QueryStr;
import utils.URL;
import utils.URLBuilder;

/**
 *
 * @author dingnangen
 */
@Named(value = "operationrecordViewMB")
@SessionScoped
public class OperationrecordViewMB implements Serializable {

    @EJB(beanName = "ProblemManager")
    private InterfaceServiceLocal<Problem> problemManager;
    @EJB(beanName = "OperationrecordManager")
    private InterfaceServiceLocal<Operationrecord> operationrecordManager;
    @EJB(beanName = "AttendanceManager")
    private InterfaceServiceLocal<Attendance> attendanceManager;
    @EJB(beanName = "CustomerdemandManager")
    private ICustomerdemandManager<Customerdemand> customerdemandManager;
    @Inject
    private SolutionViewMB solutionViewMB;

    private Problem currentProblem;
    private Operationrecord operationrecord;
    private int status;
    private String idproblem;
    private Solution solution;
    List<Customerdemand> customerdemandlist;
    List<Solution> solutionlist;
    List<Attendance> attendancelist;

    /**
     * Creates a new instance of ProblemMB
     */
    public OperationrecordViewMB() {
    }

    /**
     * 初始化
     */
    public void init() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        ArrayList pList = new ArrayList();
        pList.add(Integer.parseInt(idproblem));
        currentProblem = problemManager.find(Integer.parseInt(idproblem));
        //查找执行记录
        operationrecord = currentProblem.getIdsolution().getOperationrecord();
        customerdemandlist = new ArrayList(currentProblem.getCustomerdemandList());
        solutionlist = new ArrayList(currentProblem.getSolutionList());
        attendancelist = new ArrayList(currentProblem.getAttendanceList());

        System.out.println("operationrecord:" + operationrecord);
        if (operationrecord == null) {
            status = -1;
        } else {
            status = operationrecord.getState();
        }
        solution = currentProblem.getIdsolution();
        solution.setTaskList(new ArrayList(solution.getTaskList()));
        for (Task t : solution.getTaskList()) {
            ArrayList<TaskCustomerdemand> taskCustomerdemandList = new ArrayList(t.getTaskCustomerdemandList());
            t.setTaskCustomerdemandList(taskCustomerdemandList);
//                double TimeCost = tl.stream().mapToDouble(s -> s.getTimeCost()).reduce((s1, s2) -> s1 + s2).getAsDouble();
            t.setRealcap((int) taskCustomerdemandList.stream().mapToDouble(s -> s.getIdcustomerdemand().getRequirement()).reduce((s1, s2) -> s1 + s2).getAsDouble());
            //paixu
            Collections.sort(t.getTaskCustomerdemandList(), solutionViewMB.new TaskCustomerdemandComparator());;
        }
    }

    public void calculate() {
        String submitCalculateState = submitCalculate(currentProblem);
        System.out.println("submitCalculateState:" + submitCalculateState);
        if (submitCalculateState.equals("0")) {
            currentProblem = problemManager.find(Integer.parseInt(idproblem));
            solutionlist = new ArrayList(currentProblem.getSolutionList());
            Messages.create(Faces.getResourceBundle("bundle").getString("submit") + Faces.getResourceBundle("bundle").getString("success")).add();
        } else {
            Messages.create(Faces.getResourceBundle("bundle").getString("failed")).add();
        }

    }

    public Problem getCurrentProblem() {
        return currentProblem;
    }

    public void setCurrentProblem(Problem currentProblem) {
        this.currentProblem = currentProblem;
    }

    public String getIdproblem() {
        return idproblem;
    }

    public void setIdproblem(String idproblem) {
        this.idproblem = idproblem;
    }

    public List<Customerdemand> getCustomerdemandlist() {

        return customerdemandlist;
    }

    public void setCustomerdemandlist(List<Customerdemand> customerdemandlist) {
        this.customerdemandlist = customerdemandlist;
    }

    public List<Solution> getSolutionlist() {
        return solutionlist;
    }

    public void setSolutionlist(List<Solution> solutionlist) {
        this.solutionlist = solutionlist;
    }

    public List<Attendance> getAttendancelist() {
        return attendancelist;
    }

    public void setAttendancelist(List<Attendance> attendancelist) {
        this.attendancelist = attendancelist;
    }

    private String submitCalculate(Problem problem) {
        String url = new URLBuilder(URL.CALCULATE_URL).addParam("idproblem", "" + problem.getIdproblem()).build();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response;
        try {
            response = client.newCall(request).execute();
            String result = response.body().string();
            System.out.println("response:" + result);
            return result;
        } catch (IOException ex) {
            Logger.getLogger(ProblemCreateMB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "1";

    }

    public Operationrecord getOperationrecord() {
        return operationrecord;
    }

    public void setOperationrecord(Operationrecord operationrecord) {
        this.operationrecord = operationrecord;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Solution getSolution() {
        return solution;
    }

    public void setSolution(Solution solution) {
        this.solution = solution;
    }

}
